// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import Router from 'vue-router'
import {routers, appRouter, OrtherPage} from './router'
import store from './vuex/store'
import VueResource from 'vue-resource'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import comjs from './assets/commonJS/com'
import moment from 'moment'
import cascaderMulti from "cascader-multi";

Vue.use(cascaderMulti);
Vue.use(Router)
Vue.use(comjs)
Vue.use(Vuex)
Vue.use(iView)
Vue.use(VueResource)
Vue.config.productionTip = false
const router             = new Router({routes: routers});

//全局过滤器
Vue.filter('fmtDate', (input, formatString) => {
  const lastFormatString = formatString || "YYYY年MM月DD日"
  return moment(input).format(lastFormatString)
})

function getRouterObjByName(routers, name) {
  let routerObj = {};
  routers.forEach(item => {
    if (item.name === 'home') {
      item.children.forEach((child, i) => {
        if (child.name === name) {
          routerObj = item.children[i];
        }
      });
    } else {
      if (item.children.length === 1) {
        if (item.children[0].name === name) {
          routerObj = item.children[0];
        }
      } else {
        item.children.forEach((child, i) => {
          if (child.name === name) {
            routerObj = item.children[i];
          }
        });
      }
    }
  });
  return routerObj;
};
router.beforeEach((to, from, next) => {
  const page = getRouterObjByName([OrtherPage, ...appRouter], to.name);

  if (to.meta.requireAuth) {
    if (window.sessionStorage.getItem('ZHJF')) {
      next();
    } else {
      next({
        path : '/login',
        query: {redirect: to.fullPath}
      })
    }
  }
  else {
    next();
  }
})


/* eslint-disable no-new */
new Vue({
  el        : '#app',
  router,
  store,
  template  : '<App/>',
  components: {App},
  mounted() {
    document.getElementById('ServerTitle').innerText = this._SERVEROBJ.title
    this.$store.commit('setMenuList', appRouter)
  }
})
